package Frame;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.*;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileSystemView;

import Function.FCAnimationLibrary;
import VWorldGameStudio.FCAnimation;
import lombok.Getter;
import lombok.Setter;

/**
 * 怪物攻击动画设计窗口。 分为四点，动作起点、转折点1、转折点2、终点。 起点：如古炮发射的炮弹出现位置；
 * 转折点1：如古炮发射时，古炮与主角不在同一水平线上，炮弹离开古炮后转折飞行路线的第一个转折位置； 转折点2：如弹弓发射的石子，碰到怪物时的接触位置转折点；
 * 终点：如弹弓在第二个转折点开始后，石子继续滑行到消失的位置。 Static类型，为无动画，只是怪物或角色后退一下，只需设置a点。
 * Move类型：有动画，需要设置abcd点（对应四点）。
 */
public class FCEditAnimation extends JDialog {

    private static final String SET_STR = "点设置";
    private static final String CLICK_TO_SET = "点击设置";
    private static final int POINT_X = 10;
    private static final int POINT_WIDTH = 190;
    private static final int POINT_HEIGHT = 20;

    // 攻击动画素材
    private Vector<String> pngUrl = new Vector<>();
    // TODO
    private String[] pngUrl2 = {"", "", "", ""};
    // 行为动作库
    private FCAnimationLibrary animationLib;
    // 编辑器主窗口
    private FCGMaster master;
    // 图片素材列表显示首位编号
    private int firstnumofta = 0;
    FCAnimation fcAnimation;//再次编辑的

    static int pressWay = -1;
    static ImageEdit globalImgEdit;
    static Timer timer;
    private boolean isNew = true;

    // 文件选择
    private JFileChooser fileChooser = new JFileChooser(new File("./material/png/animation/")/*FileSystemView.getFileSystemView().getHomeDirectory()*/) {
        protected javax.swing.JDialog createDialog(java.awt.Component parent) throws java.awt.HeadlessException {
            // 创建对话框
            JDialog fileChooserDialog = super.createDialog(parent);
            // 设置属性
            fileChooserDialog.setSize(500, 400);
            fileChooserDialog.setTitle("选择文件");
            fileChooserDialog.setResizable(false);
            fileChooserDialog.setLocationRelativeTo(null);
            return fileChooserDialog;
        }
    };

    @Override
    public void dispose() {
        super.dispose();
    }

    /**
     * 构造方法初始化
     *
     * @param isnew        判断是否为新增
     * @param animationLib 行为动作库
     * @param master       编辑器主窗口
     */
    public FCEditAnimation(boolean isnew, FCAnimationLibrary animationLib, FCGMaster master) {
        this.animationLib = animationLib;
        this.master = master;
        this.isNew = isnew;
        // 设置窗体基本属性
        setTitle((isnew) ? "创建动画" : "编辑动画");
        setBounds(0, 0, 775, 625);
        setLayout(null);
        setResizable(false);
        setLocationRelativeTo(null);
        setModal(true);

        if (timer == null) {
            timer = new Timer();
            timer.schedule(new MoveImg(), new Date(), 50);
        }

        // 设置攻击动画IDlabel
        JLabel animaIdLabel = new JLabel("id");
        animaIdLabel.setBounds(10, 10, 40, 20);
        this.add(animaIdLabel);

        JTextField animationId = new JTextField();
        animationId.setBounds(50, 10, 150, 20);

        JComboBox<String> animationIdBox = new JComboBox<String>();
        // 设置攻击动画
        if (isnew) {
            this.add(animationId);
        } else {
            animationIdBox.setBounds(50, 10, 150, 20);
            for (String s : FCGMaster.getAnimationLibrary().getAnimationId()) {
                animationIdBox.addItem(s);
            }
            this.add(animationIdBox);
        }
//        // 设置攻击动画类型label
//        JLabel animaTypeLabel = new JLabel("类型");
//        animaTypeLabel.setBounds(210, 10, 40, 20);
//        this.add(animaTypeLabel);
//
//        // 设置攻击动画类型
//        JComboBox<String> animationType = new JComboBox<String>();
//        animationType.setBounds(250, 10, 150, 20);
//        animationType.addItem("static");
//        animationType.addItem("move");
//        animationType.setSelectedItem("static");
//        this.add(animationType);

        // 设置动画四个点
        JLabel aPointLabel = new JLabel("开头动画" + SET_STR);
        aPointLabel.setBounds(POINT_X, 40, POINT_WIDTH, POINT_HEIGHT);
        this.add(aPointLabel);

        JTextField aPoint = new JTextField(CLICK_TO_SET);
        aPoint.setBounds(POINT_X, 70, POINT_WIDTH, POINT_HEIGHT);
        aPoint.setEditable(false);
        this.add(aPoint);

        JLabel bPointLabel = new JLabel("中间动画" + SET_STR);
        bPointLabel.setBounds(POINT_X, 100, POINT_WIDTH, POINT_HEIGHT);
        this.add(bPointLabel);

        JTextField bPoint = new JTextField(CLICK_TO_SET);
        bPoint.setBounds(POINT_X, 130, POINT_WIDTH, POINT_HEIGHT);
        bPoint.setEditable(false);
        this.add(bPoint);

        JLabel bPointLabel2 = new JLabel("中间动画2(4段动画d需要)" + SET_STR);
        bPointLabel2.setBounds(POINT_X, 160, POINT_WIDTH, POINT_HEIGHT);
        this.add(bPointLabel2);
        JTextField bPoint2 = new JTextField(CLICK_TO_SET);
        bPoint2.setBounds(POINT_X, 190, POINT_WIDTH, POINT_HEIGHT);
        bPoint2.setEditable(false);
        this.add(bPoint2);
        bPoint2.setEnabled(false);

        JLabel cPointLabel = new JLabel("结尾动画" + SET_STR);
        cPointLabel.setBounds(POINT_X, 210, POINT_WIDTH, POINT_HEIGHT);
        this.add(cPointLabel);

        JTextField cPoint = new JTextField(CLICK_TO_SET);
        cPoint.setBounds(POINT_X, 240, POINT_WIDTH, POINT_HEIGHT);
        cPoint.setEditable(false);
        this.add(cPoint);
        // 设置动画四个点结束

        // TODO 图片素材列表
        JPanel ta = new JPanel() {
            @Override
            public void paint(Graphics g) {
                tapaint(g, this.getWidth(), this.getHeight());
            }
        };
        ta.setBounds(10, 280, 190, 240);
        this.add(ta);
        ta.addMouseWheelListener(new MouseWheelListener() {
            @Override
            public void mouseWheelMoved(MouseWheelEvent e) {
                firstnumofta += e.getWheelRotation();
                if (firstnumofta < 0)
                    firstnumofta = 0;
                if (firstnumofta >= pngUrl.size())
                    firstnumofta = pngUrl.size() - 1;
                ta.paint(ta.getGraphics());
            }
        });

        // 添加素材按钮
        JButton addButton = new JButton("添加素材");
        addButton.setBounds(10, 530, 190, 20);
        this.add(addButton);


        JLabel scalet = new JLabel("结尾动画放大倍数");
        scalet.setBounds(220, 530, 120, 20);
        this.add(scalet);
        JTextField scale = new JTextField();
        scale.setBounds(340, 530, 60, 20);
        this.add(scale);
        scale.setText("1");

        // TODO
        JPanel pa = new JPanel() {
            @Override
            public void paint(Graphics g) {
                papaint(g);
            }
        };
        pa.setBounds(210, 40, 420, 230);
        this.add(pa);

        // 确定按钮
        JButton okButton = new JButton("确定");
        okButton.setBounds(10, 560, 750, 20);
        this.add(okButton);


        JLabel copyani = new JLabel("复制动画(名称)");
        copyani.setBounds(420, 530, 100, 20);
        this.add(copyani);
        JTextField copyaniT = new JTextField();
        copyaniT.setBounds(520, 530, 130, 20);
        this.add(copyaniT);
        JButton copyB = new JButton("复制");
        copyB.setBounds(670, 530, 100, 20);
        this.add(copyB);

        copyB.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(!isnew){
                    String copyName = copyaniT.getText();
                    if(fcAnimation != null && copyName != null && copyName.length() > 1){
                        String oldName = fcAnimation.getID();
                        FCAnimation copyA = fcAnimation;
                        copyA.setId(copyName);
                        animationLib.registerAnimation(true, copyA);
                        new FCTip("提示", "复制成功");
                        fcAnimation.setId(oldName);
                    }
                }
            }
        });

        // 设置四个点的点击事件
        aPoint.addMouseListener(new Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new selectInfo(aPoint, 0);
            }
        });
        bPoint.addMouseListener(new Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new selectInfo(bPoint, 1);
            }
        });
//        bPoint2.addMouseListener(new Mlistener() {
//            @Override
//            public void mouseClicked(MouseEvent e) {
//                new selectInfo(bPoint2, 2);
//            }
//        });
        cPoint.addMouseListener(new Mlistener() {
            @Override
            public void mouseClicked(MouseEvent e) {
                new selectInfo(cPoint, 3);
            }
        });
        // 设置四个点的点击事件结束

        // 添加按钮监听器
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                if (fileChooser.showOpenDialog(null) == fileChooser.APPROVE_OPTION) {
//					pngUrl.add(fileChooser.getSelectedFile().getPath().replaceAll("\\\\", "/") + "&"
//							+ fileChooser.getSelectedFile().getName());
                    pngUrl.clear();
                    File file = new File(fileChooser.getSelectedFile().getParent());
                    String path = fileChooser.getSelectedFile().getParent().replaceAll("\\\\", "/");
                    System.out.println("--" + path);
                    File[] files = file.listFiles();
                    if (files != null) {
                        for (File f : files) {
                            pngUrl.add(path + "&" + f.getName());
                        }
                    }
                    ta.repaint();
                }
            }
        });

        // 确认按钮监听器
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (animationId.getText() != null && !animationId.getText().equals("")) {
                    if (!isNew || !animationLib.isRepeat(animationId.getText())) {
                        boolean isok = true;
                        if (aPoint.getText().equals(CLICK_TO_SET) || aPoint.getText().equals("")) {
                            isok = false;
                        }
                        if (isok) {
//                                StringBuilder stringBuilder = new StringBuilder();
//                                stringBuilder.append(animationId.getText() + " " + animationType.getSelectedItem().toString());
//                                stringBuilder.append(", " + aPoint.getText());
//                                System.out.println(stringBuilder.toString());
                            FCAnimation animation = new FCAnimation();
                            animation.setId(animationId.getText());
                            animation.setType("move"/*animationType.getSelectedItem().toString()*/);
                            //大小，偏移，音乐，图片
                            String[] splits = aPoint.getText().split(";");
                            if (splits[0] != null && splits[0].length() > 0) {
                                animation.fireFrameTime = Integer.parseInt(splits[0]);
                            }
                            String[] s = splits[1].split("x");
                            animation.firePngSizeW = Integer.parseInt(s[0]);
                            animation.firePngSizeH = Integer.parseInt(s[1]);
                            animation.setAnimationForWho(Integer.parseInt(s[2]));

                            if (splits[2] != null && splits[2].contains("x")) {
                                String[] s2 = splits[2].split("x");
                                animation.firePngCenterOffsetX = Integer.parseInt(s2[0]);
                                animation.firePngCenterOffsetY = Integer.parseInt(s2[1]);
                            }
                            animation.setFireMusicId(Integer.parseInt(splits[3]));
                            int len = splits.length - 4;
                            if (len > 0) {
                                for (int i = 4; i < splits.length; i++) {
                                    String pngName = splits[i].substring(splits[i].lastIndexOf("/"));
                                    animation.getFirePngUrl().add("./material/png/animation" + pngName);
                                    System.out.println("====" + pngName);
                                }
                            }
                            //222
                            if (!bPoint.getText().equals(CLICK_TO_SET) && !bPoint.getText().equals("")) {
                                String[] splits2 = bPoint.getText().split(";");
                                String[] ss1 = splits2[1].split("x");
                                animation.flyingPngSizeW = Integer.parseInt(ss1[0]);
                                animation.flyingPngSizeH = Integer.parseInt(ss1[1]);
                                if (splits2[2] != null && splits2[2].contains("x")) {
                                    String[] ss2 = splits2[2].split("x");
                                    animation.flyingPngCenterOffsetX = Integer.parseInt(ss2[0]);
                                    animation.flyingPngCenterOffsetY = Integer.parseInt(ss2[1]);
                                }
                                animation.setFlyMusicId(Integer.parseInt(splits2[3]));
                                len = splits2.length - 4;
                                if (len > 0) {
                                    for (int i = 4; i < splits2.length; i++) {
                                        String pngName = splits2[i].substring(splits2[i].lastIndexOf("/"));
                                        animation.getFlyingPngUrl().add("./material/png/animation" + pngName);
                                    }
                                }
                            }

                            //333
                            if (!cPoint.getText().equals(CLICK_TO_SET) && !cPoint.getText().equals("")) {
                                String[] splits3 = cPoint.getText().split(";");
                                if (splits3[0] != null && splits3[0].length() > 0) {
                                    animation.boomFrameTime = Integer.parseInt(splits3[0]);
                                }
                                String[] sss1 = splits3[1].split("x");
                                animation.boomPngSizeW = Integer.parseInt(sss1[0]);
                                animation.boomPngSizeH = Integer.parseInt(sss1[1]);
                                if (splits3[2] != null && splits3[2].contains("x")) {
                                    String[] sss2 = splits3[2].split("x");
                                    animation.boomPngCenterOffsetX = Integer.parseInt(sss2[0]);
                                    animation.boomPngCenterOffsetY = Integer.parseInt(sss2[1]);
                                }
                                animation.setFlyMusicId(Integer.parseInt(splits3[3]));
                                len = splits3.length - 4;
                                if (len > 0) {
                                    for (int i = 4; i < splits3.length; i++) {
                                        String pngName = splits3[i].substring(splits3[i].lastIndexOf("/"));
                                        animation.getBoomPngUrl().add("./material/png/animation" + pngName);
                                    }
                                }
                            }
                            if (!bPoint2.getText().equals(CLICK_TO_SET) && !bPoint2.getText().equals("")) {
                                String[] bsplits2 = bPoint2.getText().split(";");
                                String[] bss1 = bsplits2[1].split("x");
                                animation.flyingPngSizeW2 = Integer.parseInt(bss1[0]);
                                animation.flyingPngSizeH2 = Integer.parseInt(bss1[1]);
                                if (bsplits2[2] != null && bsplits2[2].contains("x")) {
                                    String[] bss2 = bsplits2[2].split("x");
                                    animation.flyingPngCenterOffsetX2 = Integer.parseInt(bss2[0]);
                                    animation.flyingPngCenterOffsetY2 = Integer.parseInt(bss2[1]);
                                }
                                animation.setFlyMusicId2(Integer.parseInt(bsplits2[3]));
                                len = bsplits2.length - 4;
                                if (len > 0) {
                                    for (int i = 4; i < bsplits2.length; i++) {
                                        String pngName = bsplits2[i].substring(bsplits2[i].lastIndexOf("/"));
                                        animation.getFlyingPngUrl2().add("./material/png/animation" + pngName);
                                    }
                                }
                            }
                            animation.boomPngScales = Double.parseDouble(scale.getText());
                            animationLib.registerAnimation(true, animation);
                            new FCTip("提示", "保持成功");
                            dispose();
                        } else {
                            new FCTip("攻击动画编辑提示", "最少要填完开头动画，中间动画，结尾动画");
                        }
                    } else {
                        new FCTip("攻击动画编辑提示", "id重复");
                    }
                } else {
                    new FCTip("攻击动画编辑提示", "id不得为空");
                }
            }
        });

        if (!isnew) {
            animationIdBox.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    if (animationIdBox.getSelectedItem() != null) {
                        animationId.setText((String) animationIdBox.getSelectedItem());
                        //TODO 并且填充所有的数据
                        fcAnimation = animationLib.getAnimationOfID(animationId.getText());
                        StringBuilder stringBuilder = new StringBuilder();
                        stringBuilder.append(fcAnimation.fireFrameTime);
                        stringBuilder.append(";" + fcAnimation.firePngSizeW + "x" + fcAnimation.firePngSizeW + "x" + fcAnimation.getAnimationForWho());
                        stringBuilder.append(";" + fcAnimation.firePngCenterOffsetX + "x" + fcAnimation.firePngCenterOffsetY);
                        stringBuilder.append(";" + fcAnimation.getFireMusicId());
                        if (fcAnimation.getFirePngUrl().size() == 0) {
                            stringBuilder.append(";");
                        } else {
                            for (String f : fcAnimation.getFirePngUrl()) {
                                stringBuilder.append(";" + f);
                            }
                        }
                        aPoint.setText(stringBuilder.toString());
                        //Bpoint
                        stringBuilder = new StringBuilder();
                        stringBuilder.append(0);
                        stringBuilder.append(";" + fcAnimation.flyingPngSizeW + "x" + fcAnimation.flyingPngSizeH + "x" + fcAnimation.getAnimationForWho());
                        stringBuilder.append(";" + fcAnimation.flyingPngCenterOffsetX + "x" + fcAnimation.flyingPngCenterOffsetY);
                        stringBuilder.append(";" + fcAnimation.getFlyMusicId());
                        if (fcAnimation.getFlyingPngUrl().size() == 0) {
                            stringBuilder.append(";");
                        } else {
                            for (String f : fcAnimation.getFlyingPngUrl()) {
                                stringBuilder.append(";" + f);
                            }
                        }
                        bPoint.setText(stringBuilder.toString());
                        //bpint2
                        stringBuilder = new StringBuilder();
                        stringBuilder.append(0);
                        stringBuilder.append(";" + fcAnimation.flyingPngSizeW2 + "x" + fcAnimation.flyingPngSizeH2 + "x" + fcAnimation.getAnimationForWho());
                        stringBuilder.append(";" + fcAnimation.flyingPngCenterOffsetX2 + "x" + fcAnimation.flyingPngCenterOffsetY2);
                        stringBuilder.append(";" + fcAnimation.getFlyMusicId2());
                        if (fcAnimation.getFlyingPngUrl2().size() == 0) {
                            stringBuilder.append(";");
                        } else {
                            for (String f : fcAnimation.getFlyingPngUrl2()) {
                                stringBuilder.append(";" + f);
                            }
                        }
                        bPoint2.setText(stringBuilder.toString());
                        //dpoint
                        stringBuilder = new StringBuilder();
                        stringBuilder.append(fcAnimation.boomFrameTime);
                        stringBuilder.append(";" + fcAnimation.boomPngSizeW + "x" + fcAnimation.boomPngSizeH + "x" + fcAnimation.getAnimationForWho());
                        stringBuilder.append(";" + fcAnimation.boomPngCenterOffsetX + "x" + fcAnimation.boomPngCenterOffsetY);
                        stringBuilder.append(";" + fcAnimation.getBoomMusicId());
                        if (fcAnimation.getBoomPngUrl().size() == 0) {
                            stringBuilder.append(";");
                        } else {
                            for (String f : fcAnimation.getBoomPngUrl()) {
                                stringBuilder.append(";" + f);
                            }
                        }
                        cPoint.setText(stringBuilder.toString());

                        scale.setText(String.valueOf(fcAnimation.boomPngScales));
                    }
                }
            });
        }
        // 设置窗体可见
        setVisible(true);
    }

    /**
     * 绘制添加的图片素材列表
     *
     * @param g
     */
    private void tapaint(Graphics g, int w, int h) {
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
        g.setColor(Color.black);
        g.drawRect(0, 0, w - 1, h - 1);
        for (int i = firstnumofta; i < pngUrl.size(); i++) {
            g.drawString(pngUrl.get(i).split("&")[1], 5, 16 + 18 * (i - firstnumofta));
            g.drawLine(1, 20 + 18 * (i - firstnumofta), w, 20 + 18 * (i - firstnumofta));
            g.setColor(Color.cyan);
            g.fillRect(w - 19, 20 * (i - firstnumofta) + 2, 18, 18);
            g.setColor(Color.black);
        }
    }

    /**
     * @param g
     */
    private void papaint(Graphics g) {
        g.setColor(Color.black);
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.setColor(Color.white);
        g.drawLine(10, 20, 10, this.getHeight() - 60);
        g.drawLine(110, 20, 110, this.getHeight() - 60);
        g.drawLine(310, 20, 310, this.getHeight() - 60);
        g.drawLine(410, 20, 410, this.getHeight() - 60);
    }

    private class selectInfo extends JDialog {
        private String selecturl = "";
        private String selectname = "";
        private int firstnum = 0;

        public selectInfo(JTextField jt, int num) {
            setBounds(0, 0, 1500, 900);
            setTitle("设置参数");
            setModal(true);
            setLocationRelativeTo(null);
            setLayout(null);
            setResizable(false);

            JComboBox<String> attackWay = new JComboBox<String>();
            attackWay.setBounds(10, 20, 120, 20);
            attackWay.addItem("人类武器动画");
            attackWay.addItem("坦克武器动画");
            this.add(attackWay);

            JLabel a2 = new JLabel("动画素材");
            a2.setBounds(10, 65, 80, 20);
            this.add(a2);
            JComboBox<String> a21 = new JComboBox<String>();
            a21.setBounds(80, 65, 140, 20);
            for (String str : pngUrl)
                a21.addItem(str.split("&")[1]);
            this.add(a21);
            JLabel pp = new JLabel("+", JLabel.CENTER);
            pp.setFont(new Font("黑体", 0, 18));
            pp.setBorder(BorderFactory.createLineBorder(Color.black));
            pp.setBounds(225, 65, 20, 20);
            this.add(pp);

            JPanel p = new JPanel() {
                @Override
                protected void paintComponent(Graphics g) {
                    g.setColor(Color.white);
                    g.fillRect(0, 0, 200, 700);
                    String a[] = selectname.split(";");
                    g.setColor(Color.black);
                    for (int i = firstnum; i < a.length; i++) {
                        g.drawString(a[i], 5, i * 20 + 17);
                        g.drawLine(0, i * 20 + 20, 200, i * 20 + 20);
                        g.setColor(Color.white);
                        g.fillRect(180, i * 20 + 1, 20, 18);
                        g.setColor(Color.black);
                        g.drawLine(184, i * 20 + 10, 193, i * 20 + 10);
                    }
                }
            };
            p.setBounds(260, 50, 200, 700);
            p.setBorder(BorderFactory.createLineBorder(Color.black));
            this.add(p);

            JLabel a3 = new JLabel("音乐");
            a3.setBounds(10, 90, 40, 20);
            this.add(a3);
            JComboBox<String> a31 = new JComboBox<String>();
            a31.setBounds(50, 90, 120, 20);
            a31.addItem("-1");
            for (int i = 0; i < 30; i++)
                a31.addItem(String.valueOf(i));
            this.add(a31);


            JLabel a5 = new JLabel("动画表演时间默认300毫秒");
            a5.setBounds(10, 180, 200, 20);
            if (num != 1 && num != 2) {
                this.add(a5);
            }
            JTextField a51 = new JTextField();
            a51.setBounds(10, 200, 120, 20);
            if (num != 1 && num != 2) {
                this.add(a51);
            }
            // 确认按钮
            JButton okButton = new JButton("确认");
            okButton.setBounds(10, 230, 160, 20);
            this.add(okButton);

            ImageEdit imageEdit = new ImageEdit(num);
            imageEdit.setBounds(484, 50, 900, 750);
            imageEdit.setOrginUrl("./material/png/character/player1-pistol.png", 1);
            this.add(imageEdit);

            JLabel monsterWayt = new JLabel("要是在怪物攻击的在这里选择怪物");
            monsterWayt.setBounds(10, 800, 200, 20);
            this.add(monsterWayt);
            JComboBox<String> monsterWay = new JComboBox<String>();
            monsterWay.setBounds(218, 800, 120, 20);
            monsterWay.addItem("null");
            HashMap<String, String> pngMap = new HashMap<>();
            File[] files = new File("./material/png/monster").listFiles();
            if (files != null) {
                for (File f : files) {
                    pngMap.put(f.getName(), f.getAbsolutePath());
                    monsterWay.addItem(f.getName());
                }
            }
            this.add(monsterWay);

            attackWay.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if ("人类武器动画".equals(attackWay.getSelectedItem())) {
                        imageEdit.setOrginUrl("./material/png/character/player1-pistol.png", 1);
                    } else if ("坦克武器动画".equals(attackWay.getSelectedItem())) {
                        imageEdit.setOrginUrl("./material/png/tank/A-honglan.png", 2);
                    }
                    imageEdit.updateUI();
                }
            });
            monsterWay.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    String file = monsterWay.getSelectedItem().toString();
                    String path = pngMap.get(file);
                    if (path != null && !path.equals("null")) {
                        imageEdit.setOrginUrl(path, 3);
                    }
                    imageEdit.updateUI();
                }
            });
            imageEdit.setBackground(Color.BLACK);
            globalImgEdit = imageEdit;
            pp.addMouseListener(new Mlistener() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    selecturl += pngUrl.get(a21.getSelectedIndex()).replace("&", "/") + ";";
                    selectname += pngUrl.get(a21.getSelectedIndex()).split("&")[1] + ";";
                    imageEdit.setAnimateImgUrl(selecturl.split(";")[0]);
                    imageEdit.updateUI();
                    p.updateUI();
                }
            });

            if (fcAnimation != null) {
                ArrayList<String> urls = fcAnimation.getFirePngUrl();
                String offset = fcAnimation.firePngCenterOffsetX + "x" + fcAnimation.firePngCenterOffsetY;
                if (num == 0) {
                    a5.setText(String.valueOf(fcAnimation.fireFrameTime));
                } else if (num == 1) {
                    urls = fcAnimation.getFlyingPngUrl();
                    a5.setText(String.valueOf("0"));
                    offset = fcAnimation.flyingPngCenterOffsetX + "x" + fcAnimation.flyingPngCenterOffsetY;
                } else if (num == 2) {
                    urls = fcAnimation.getFlyingPngUrl2();
                    a5.setText(String.valueOf("0"));
                    offset = fcAnimation.flyingPngCenterOffsetX2 + "x" + fcAnimation.flyingPngCenterOffsetY2;
                } else if (num == 3) {
                    urls = fcAnimation.getBoomPngUrl();
                    a5.setText(String.valueOf(fcAnimation.boomFrameTime));
                    offset = fcAnimation.boomPngCenterOffsetX + "x" + fcAnimation.boomPngCenterOffsetY;
                }
                if (urls.size() > 0) {
                    if (globalImgEdit.getType() == 1) {
                        imageEdit.setOrginUrl("./material/png/character/player1-pistol.png", 1);
                    } else if (globalImgEdit.getType() == 2) {
                        imageEdit.setOrginUrl("./material/png/tank/A-honglan.png", 2);
                    } else if (globalImgEdit.getType() == 3) {
                        imageEdit.setOrginUrl("./material/png/monster/bote.png", 3);
                    }

                    selecturl = urls.get(0);
                    globalImgEdit.setAnimateImgUrl(selecturl);
                    globalImgEdit.resetOffset(offset);
                    selectname = selecturl.substring(selecturl.lastIndexOf("/"));
                    for (int i = 1; i < urls.size(); i++) {
                        String path = urls.get(i);
                        selecturl = selecturl + ";" + path;
                        selectname = selectname + ";" + path.substring(path.lastIndexOf("/"));
                    }
                }
                globalImgEdit.updateUI();
            }

            p.addMouseListener(new Mlistener() {
                @Override
                public void mouseEntered(MouseEvent e) {
                        System.out.println("ssss");
                }

                @Override
                public void mouseClicked(MouseEvent e) {
                    if (e.getX() > 180) {
                        int a = e.getY() / 20;
                        String q[] = selecturl.split(";");
                        String w[] = selectname.split(";");
                        selecturl = "";
                        selectname = "";
                        for (int i = 0; i < q.length; i++)
                            if (a != i) {
                                selecturl += q[i] + ";";
                                selectname += w[i] + ";";
                            }
                        p.updateUI();
                    }
                }
            });
            p.addMouseWheelListener(new MouseWheelListener() {
                @Override
                public void mouseWheelMoved(MouseWheelEvent e) {
                    firstnum += e.getWheelRotation();
                    if (firstnum < 0)
                        firstnum = 0;
                    if (firstnum >= selecturl.split(";").length)
                        firstnum--;
                }
            });
            // 确认按钮监听器
            okButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //表演时间，大小，偏移，音乐，图片
                    jt.setText(a51.getText() + ";" + globalImgEdit.aniImgW + "x" + globalImgEdit.aniImgH + "x" + globalImgEdit.getType() + ";" + globalImgEdit.getPianyi() + ";" + a31.getSelectedItem().toString() +
                            ";" + selecturl);
                    pngUrl2[num] = selecturl;
                    System.out.println("selecturl:" + selecturl);
                    dispose();
                }
            });

            JLabel l = new JLabel("-", JLabel.CENTER);
            l.setFont(new Font("黑体", 0, 18));
            l.setBorder(BorderFactory.createLineBorder(Color.black));
            l.setBounds(460, 425, 20, 20);
            this.add(l);
            JLabel d = new JLabel("-", JLabel.CENTER);
            d.setFont(new Font("黑体", 0, 18));
            d.setBorder(BorderFactory.createLineBorder(Color.black));
            d.setBounds(934, 810, 20, 20);
            this.add(d);
            JLabel u = new JLabel("+", JLabel.CENTER);
            u.setFont(new Font("黑体", 0, 18));
            u.setBorder(BorderFactory.createLineBorder(Color.black));
            u.setBounds(934, 30, 20, 20);
            this.add(u);
            JLabel r = new JLabel("+", JLabel.CENTER);
            r.setFont(new Font("黑体", 0, 18));
            r.setBorder(BorderFactory.createLineBorder(Color.black));
            r.setBounds(1390, 425, 20, 20);
            this.add(r);
            l.addMouseListener(new Mlistener2() {
                @Override
                public void mousePressed(MouseEvent e) {
                    pressWay = 0;
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    pressWay = -1;
                }
            });
            r.addMouseListener(new Mlistener2() {
                @Override
                public void mousePressed(MouseEvent e) {
                    pressWay = 1;
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    pressWay = -1;
                }
            });
            u.addMouseListener(new Mlistener2() {
                @Override
                public void mousePressed(MouseEvent e) {
                    pressWay = 2;
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    pressWay = -1;
                }
            });
            d.addMouseListener(new Mlistener2() {
                @Override
                public void mousePressed(MouseEvent e) {
                    pressWay = 3;
                }

                @Override
                public void mouseReleased(MouseEvent e) {
                    pressWay = -1;
                }
            });
            // 设置窗体可见
            setVisible(true);
        }
    }


    class MoveImg extends TimerTask {

        @Override
        public void run() {
            if (globalImgEdit != null) {
                switch (pressWay) {
                    case 0:
                        globalImgEdit.moveLeft();
                        globalImgEdit.updateUI();
                        break;
                    case 1:
                        globalImgEdit.moveRight();
                        globalImgEdit.updateUI();
                        break;
                    case 2:
                        globalImgEdit.moveUp();
                        globalImgEdit.updateUI();
                        break;
                    case 3:
                        globalImgEdit.modeDown();
                        globalImgEdit.updateUI();
                        break;
                }
            }
        }
    }

    class ImageEdit extends JPanel {

        public ImageEdit(int num) {
            this.num = num;
        }

        private int num;//3表示是第3阶段的动画
        private Image oImg;
        private Image animImg;
        private int imgW = 96;
        private int imgH = 102;
        private int type = 1;//1 是人类被攻击， 3是怪物被打，
        private int[] record = new int[2];
        Font font = new Font("黑体", 0, 20);

        public int getType() {
            return type;
        }

        private String hasRecord;
        private int orignX = 180;
        private int orignY = 110;
        @Getter
        private int aniImgW = 0;
        @Getter
        private int aniImgH = 0;
        private int aniImageX = 0, aniImageY = 0;

        @Override
        public void setBounds(int x, int y, int width, int height) {
            super.setBounds(x, y, width, height);
            font = new Font("黑体", 0, 18);
        }


        public void setOrginUrl(String p, int type) {
            this.type = type;
            System.out.println("setOrginUrl num:"+num+" type:"+type);
            oImg = Toolkit.getDefaultToolkit().getImage(p);
            resetAnimxY();
            if (hasRecord != null) {
                resetOffset(hasRecord);
            }
        }

        public void resetOffset(String str) {
            this.hasRecord = str;
            resetAnimxY();
            String[] strings = str.split("x");
            int rex = Integer.valueOf(strings[0]);
            int rey = Integer.valueOf(strings[1]);
            aniImageX = rex + record[0];
            aniImageY = rey + record[1];
        }

        private void resetAnimxY() {
            System.out.println("resetAnimxY num:"+num+" type:"+type);
            if (num == 3) {//别动它
                record[0] = orignX + imgW / 2;
                record[1] = orignY + imgH / 2;
                aniImageX = record[0];
                aniImageY = record[1];
            } else {
                if (type == 3) {//
                    record[0] = orignX + imgW;
                    record[1] = orignY + imgH / 2;
                    aniImageX = record[0];
                    aniImageY = record[1];
                } else {
                    record[0] = orignX;
                    record[1] = orignY + imgH / 2;
                    aniImageX = record[0];
                    aniImageY = record[1];
                }
            }
        }

        private String oldString ;
        public void setAnimateImgUrl(String p) {
            if ( p != oldString) {
                oldString = p;
                animImg = Toolkit.getDefaultToolkit().getImage(p);
                resetAnimxY();
                if (hasRecord != null) {
                    resetOffset(hasRecord);
                }
            }
        }


        public void moveUp() {
            aniImageY--;
        }

        public void modeDown() {
            aniImageY++;
        }

        public void moveRight() {
            aniImageX++;
        }

        public void moveLeft() {
            aniImageX--;
        }

        public String getPianyi() {
            return (aniImageX - record[0]) + "x" + (aniImageY - record[1]);
        }


        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            if (oImg != null) {
                if (type == 1) {
                    imgW = 96;
                    imgH = 102;
                } else {
                    //画坦克和画怪是直接放大俩倍的
                    imgW = oImg.getWidth(this) * 2;
                    imgH = oImg.getHeight(this) * 2;
                }
                int px = (getWidth() - imgW) / 2;
                int py = (getHeight() - imgH) / 2;
                if (px != orignX || py != orignY) {
                    orignX = px;
                    orignY = py;
                    resetAnimxY();
                    if (hasRecord != null) {
                        resetOffset(hasRecord);
                    }
                }
                g.drawImage(oImg, orignX, orignY, imgW, imgH, this);
            }
            g.setColor(Color.WHITE);
            if (animImg != null) {
                aniImgW = animImg.getWidth(this);
                aniImgH = animImg.getHeight(this);
                g.drawImage(animImg, aniImageX, aniImageY, aniImgW, aniImgH, this);
                g.drawLine(aniImageX, aniImageY, aniImageX + aniImgW, aniImageY);
                g.drawLine(aniImageX, aniImageY, aniImageX, aniImageY + aniImgH);
                g.drawLine(aniImageX, aniImageY + aniImgH, aniImageX + aniImgW, aniImageY + aniImgH);
                g.drawLine(aniImageX + aniImgW, aniImageY, aniImageX + aniImgW, aniImageY + aniImgH);
            }

            g.setFont(font);
            if (num == 0 || num == 1 || num == 2) {
                g.drawString("点击将动画图像移到“枪口攻击”的地方。", 20, 20);
            } else if (num == 3) {
                g.drawString("点击将动画图像移到“居中”的地方。", 20, 20);
                g.drawString("需要自行从左下测选择对应怪物（默认波特怪）", 20, 64);
            }
            g.drawString("当前偏移" + (aniImageX - record[0]) + "x" + (aniImageY - record[1]), 20, 44);
        }
    }

    public abstract static class Mlistener implements MouseListener {


        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }

    abstract class Mlistener2 implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }
}
